package com.itht.baoleme.service;

import com.itht.baoleme.domain.AccountDTO;
import com.itht.baoleme.domain.web.OrderDTO;
import com.itht.baoleme.domain.web.WebCodeEnum;
import com.itht.baoleme.domain.web.WebServiceException;
import com.itht.baoleme.domain.web.WebServiceMessage;
import com.itht.baoleme.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class AccountService {

    @Autowired
    private AccountMapper accountMapper;

    public AccountDTO getUserInfoById(Long id) {
        // 如果查询出来用户不存在，则直接返回的就是为空
        return accountMapper.getUserInfoById(id);
    }

    public WebServiceMessage<AccountDTO> getUserInfoByIdV2(Long id) {

        AccountDTO accountDTO = accountMapper.getUserInfoById(id);

        if (accountDTO == null) {
            return WebServiceMessage.error(WebCodeEnum.USER_NOT_EXIST);
        }

        return WebServiceMessage.success(accountDTO);
    }

    public void insert(String userName, String password) {
//        accountMapper.insert(userName, password);
        // 插入成功
    }

    public AccountDTO getUser(Long userId) throws WebServiceException {
        // TODO 参数校验
        AccountDTO accountDTO = accountMapper.getUserInfoById(userId);
        if (accountDTO == null) {
            // 手动抛出自定义异常
            throw new WebServiceException(WebCodeEnum.USER_NOT_EXIST);
        }
        // 手动制造的空指针异常，为了测试异常捕获
        if (accountDTO.getMobile().equals("17777006")) {
            accountDTO.setMobile("10086");
        }
        return accountDTO;
    }
}
